/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kingbull.recorder.utils;

import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * 自定义toast
 */
public class MyToast {

    /**
     * 显示 dialog
     *
     * @param context Context
     * @param resId   stringId
     */
    public static void show(Context context, int resId) {
        String msg = null;
        try {
            msg = context.getResourceManager().getElement(resId).getString();
        } catch (IOException | WrongTypeException | NotExistException e) {
            e.printStackTrace();
            msg = "未知错误！";
        }
        show(context, msg, 1500);
    }

    /**
     * 显示 dialog
     *
     * @param context Context
     * @param msg     消息内容
     */
    public static void show(Context context, String msg) {
        show(context, msg, 1500);
    }

    /**
     * 显示 dialog
     *
     * @param context Context
     * @param msg     消息内容
     * @param time    显示时间
     */
    public static void show(Context context, String msg, int time) {
        new ToastDialog(context).setText(msg).setDuration(time).show();
    }


    /**
     * 显示 dialog
     *
     * @param context Context
     * @param msg     消息内容
     */
    public static void show2(Context context, String msg) {
        ToastDialog toastDialog = new ToastDialog(context);
        StackLayout stackLayout = new StackLayout(context);
        stackLayout.setLayoutConfig(new StackLayout.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT
        ));
        StackLayout.LayoutConfig config = new StackLayout.LayoutConfig();
        config.width = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        config.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        //设置text在布局中的格式
        config.alignment = LayoutAlignment.HORIZONTAL_CENTER ;
        Text text = new Text(context);
        text.setLayoutConfig(config);

        //自动换行
        text.setMultipleLine(true);
        text.setText(msg);
        //字体颜色
        text.setTextColor(new Color(Color.getIntColor("#ffffff")));
        text.setTextSize(14, Text.TextSizeType.FP);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setPadding(80, 40, 80, 40);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShaderType(ShapeElement.RECTANGLE);
        shapeElement.setCornerRadius(120);
        //背景色 可以根据实际需求更改
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.argb(121, 0, 0, 0)));
        text.setBackground(shapeElement);

        text.setBindStateChangedListener(new Component.BindStateChangedListener() {
            private AnimatorValue showAnimatorValue;

            @Override
            public void onComponentBoundToWindow(Component component) {
                showAnimatorValue = new AnimatorValue();
                showAnimatorValue.setDuration(300);
                showAnimatorValue.setValueUpdateListener((animatorValue, v) -> {
                    component.setAlpha(v);
                    component.setTranslationY(component.getHeight() - (v * component.getHeight()));
                });
                showAnimatorValue.start();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {
                if (showAnimatorValue != null) {
                    showAnimatorValue.cancel();
                    showAnimatorValue.release();
                }
            }
        });

        stackLayout.addComponent(text);
        toastDialog.setComponent(stackLayout);
        toastDialog.setCornerRadius(99f);
        //设置透明 如果不设置 会有默认白色背景
        toastDialog.setTransparent(true);

        toastDialog.setDuration(1000).show();
    }

}
